"""
Task 2.1: Fetch and Display Posts Using GET Request

Goal: Learn to make GET requests to an API and display the results.

Exercises:
- Fetch and display all posts (first 5)
- Fetch a single post by ID
- Fetch users and display their names
"""


import httpx

# Exercise 1.1: Fetch and display all posts (first 5)
url = "https://jsonplaceholder.typicode.com/posts"
# TODO: Send GET request and display first 5 posts

response = httpx.get(url)
posts = response.json()

for i,post in enumerate(posts[:5]):
    print(post['userId'])
    print(post['id'])
    print(post['title'])
    print(post['body'])




# Exercise 1.2: Fetch a single post by ID
url = "https://jsonplaceholder.typicode.com/posts/1"
# TODO: Send GET request and display the post details

response = httpx.get(url)
post = response.json()

print(f"id:{post['id']}")
print(f"title:{post['title']}")
print(f"body:{post['body']}")

# Exercise 1.3: Fetch users and display their names
url = "https://jsonplaceholder.typicode.com/users"
# TODO: Send GET request and display user names

response = httpx.get(url)
users = response.json()

for i,user in enumerate(users, 1):
    print(f"user names:{user['username']}")